<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>用户</span>
    </div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input
          v-model="formInline.user"
          placeholder="按用户名查询"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list" style="width: 100%">
      <el-table-column type="index" label="序号" width="180" align="center" />
      <el-table-column label="头像" align="center">
        <!-- slot-scope的可以获取数据 -->
        <template slot-scope="scope">
          <img :src="scope.row.coverImage | imgUrl" />
        </template>
      </el-table-column>
      <el-table-column prop="userName" label="用户名" align="center" />
      <el-table-column prop="nickName" label="昵称" align="center" />
      <el-table-column label="是否锁定" align="center">
        <!-- slot-scope的可以获取数据 判断是否锁定 -->
        <template slot-scope="scope">
          <span>{{ scope.row.isLocked == 0 ? '否' : '是' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            icon="el-icon-remove"
            type="danger"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            icon="el-icon-lock"
            type="success"
            @click="handleLock(scope.row)"
            >{{ scope.row.isLocked == 0 ? '锁定' : '解锁' }}</el-button
          >
          <el-button
            size="mini"
            icon="el-icon-shopping-cart-2"
            type="primary"
            @click="showCart(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- current-change本身有回调参数 触发事件中就不用写了 回调参数为当前页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="loadData"
    />
    <el-dialog title="购物车" :visible.sync="dialogVisible" width="50%">
      <el-table v-loading="loading" :data="cart" style="width: 100%">
        <el-table-column type="index" label="序号" width="180" align="center" />
        <el-table-column prop="product.name" label="商品名" align="center" />
        <el-table-column label="头像" align="center">
          <!-- slot-scope的可以获取数据 -->
          <template slot-scope="scope">
            <img :src="scope.row.product.coverImage | imgUrl" />
          </template>
        </el-table-column>
        <el-table-column prop="amount" label="商品数量" align="center" />
        <el-table-column prop="product.price" label="商品价格" align="center" />
      </el-table>
    </el-dialog>
  </el-card>
</template>

<script>
import { loadUsers, delUsers, lockUsers, loadUserCart } from '../../api/users'
export default {
  data() {
    return {
      list: [],
      total: 0,
      loading: false,
      formInline: {
        user: '',
        category: ''
      },
      dialogVisible: false,
      cart: []
    }
  },
  async created() {
    this.loadData(1)
  },
  methods: {
    async loadData(page) {
      this.loading = true
      const res = await loadUsers({ page })
      this.loading = false
      this.list = res.data
      this.total = res.total
    },
    // 删除用户
    handleDelete(item) {
      delUsers(item.id).then(res => {
        this.$message.success('删除成功')
        this.loadData()
      })
    },
    // 锁定用户
    handleLock(item) {
      lockUsers(item.id, { isLocked: Boolean(!item.isLocked) }).then(res => {
        this.loadData()
      })
    },
    // 当购物中的商品在商品列表不存在时，会崩掉 ，展示用户购物车数据要先过滤出来存在即product存在的展示
    showCart(item) {
      this.dialogVisible = true
      console.log(item)
      loadUserCart(item.id).then(res => {
        this.cart = res.data.filter(item => item.product)
      })
    },
    // 查询
    async onSubmit() {
      this.loading = true
      const res = await loadUsers({
        userName: this.formInline.user
      })
      this.loading = false
      this.list = res.data
      this.total = res.total
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    }
  }
}
</script>

<style lang="scss" scoped>
img {
  width: 50%;
}
</style>
